﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HodhodNews;
using System.Configuration;
using Googler.DAL;
using HodhodNews.Aggregation;
using HodhodNews.Storage;
using HodhodNews.Extraction;
using Googler.BLL;

namespace Googler
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataContext _Context;
        public DataContext Context
        {
            get
            {
                if (_Context == null)
                {
                    _Context = new DataContext();
                }
                return _Context;
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            NewsList = StorageProvider.Insance.GetDayNews(DateTime.Now);

            renderNewsList();
            base.OnLoad(e);
            timer1.Interval = 1000 * 60 * 10;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            NewsList = StorageProvider.Insance.GetNews(dtFromDate.Value, dtToDate.Value);
            if (!string.IsNullOrWhiteSpace(txtID.Text))
            {
                long id = 0;
                long.TryParse(txtID.Text, out id);
                NewsList = NewsList.Where(n => n.OId == id).ToList();
            }
            renderNewsList();

        }

        private void renderNewsList()
        {
            //if (NewsList == null || NewsList.Count == 0)
            //{
            //    lblNewsCount.Text = "0";
            //    return;
            //}
            lblNewsCount.Text = NewsList.Count.ToString();
            newsViewer1.DataBind();
        }

        //private void Next_Click(object sender, EventArgs e)
        //{
        //    CurrentNewsIndex++;
        //    newsViewer1.DocumentText = NewsList[currentNewsIndex].RenderToHtml();
        //}

        //private void Previous_Click(object sender, EventArgs e)
        //{
        //    CurrentNewsIndex--;
        //    newsViewer1.DocumentText = NewsList[CurrentNewsIndex].RenderToHtml();
        //}

        public List<News> NewsList
        {
            get
            {
                return newsViewer1.NewsSource;
            }
            set
            {
                newsViewer1.NewsSource = value;
            }
        }
        int currentNewsIndex = 0;
        public int CurrentNewsIndex
        {
            get
            {
                return currentNewsIndex;
            }
            set
            {
                if (value < NewsList.Count & value > -1)
                    currentNewsIndex = value;
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //Browser.Print();
            newsViewer1.ShowPrintPreviewDialog();
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            ExecuteNewsSearch();
        }
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            NewsAggregationProvider.Instance.Aggregate(new AggregationOptions()
            {
                Keywords = this.Keywords
            });

            StorageProvider.Insance.Save(NewsList);

            //NewsExtractionProvider.Insance.Extract(new ExtracationOptions()
            //{

            //});
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar1.Style = ProgressBarStyle.Blocks;
            progressBar1.Visible = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ExecuteNewsSearch();
        }
        private void ExecuteNewsSearch()
        {
            if (!backgroundWorker1.IsBusy)
            {
                progressBar1.Style = ProgressBarStyle.Marquee;
                progressBar1.Visible = true;
                backgroundWorker1.RunWorkerAsync();
            }
        }
        //GoogleNewsAggregationProvider serivce;
        //public GoogleNewsAggregationProvider NewsService { get { serivce = serivce ?? new GoogleNewsAggregationProvider(Keywords); return serivce; } }
        public List<Keyword> Keywords
        {
            get
            {
                try
                {
                    return new DataContext().Keywords.ToList();//.ToArray();//"SearchKeywords"
                }
                catch
                {
                    return new List<Keyword>()
                    {
                    };
                }
            }
        }

        private void SettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SettingsForm settings = new SettingsForm();
            settings.ShowDialog();
        }

        private void newsViewer1_NewsDeleting(object sender, Controls.NewsEventArgs e)
        {
            var news = this.Context.News.Where(n => n.OId == e.Argument).FirstOrDefault();
            this.Context.DeleteObject(news);
            this.Context.SaveChanges();
            this.NewsList = StorageProvider.Insance.GetDayNews(DateTime.Now);
        }

        private void newsViewer1_NewsPreviewing(object sender, Controls.NewsEventArgs e)
        {
            var previewForm = new PreviewForm();
            var item = StorageProvider.Insance.GetById(e.Argument);
            previewForm.ShowDialog(item);
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
            }
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Maximized;
            }
        }





        //private void txtID_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    var allowedKeys = new List<Keys>{
        //        Keys.D0,Keys.D1,Keys.D2,Keys.D3,Keys.D4,Keys.D5,Keys.D6,Keys.D7,Keys.D8,Keys.D9
        //    }.ConvertAll(k => (char)k);


        //    if (!allowedKeys.Contains(e.KeyChar))
        //    {
        //        e.Handled = true;
        //    }
        //}
    }
}
